package com.param.util;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.param.dto.ParamDataDto;

public class ParseDataUtil {
	
	private static final String splitData = "--------------------<span class='pname'>{0}</span>--------------------<br/>";
	private static final String templateData = "<p class='oneResult'> 【{0}--->{1}:】获取{2}; 配置值：【{3}】 实测值：【{4}】;比对结果：{5}<br/> </p>";
	
	/**
	 *   解析集合对象数据
	 * @param extend
	 * @param array
	 * @param result
	 * @param type:0 网页抓取前解析json数据。1:返回抓取后的json数据解析到日志
	 */
	public static void  getParamDataFromList(List<ParamDataDto> array,Map<String,Object> resultMap,Map<String,Object> disSeq) {
		parseListParam(array,resultMap,null,disSeq);
		System.out.println("===>最终结果："+JSONObject.toJSONString(array));
	}
	
	/**
	 * 解析集合对象数据
	 * @param array
	 * @param i
	 */
	public static void parseListParam(List<ParamDataDto> array,Map<String,Object> resultMap,String parentName,Map<String,Object> disSeq) {
		if(array==null||array.isEmpty()) {
			return;
		}
		for (ParamDataDto paramDataDto : array) {
			if(paramDataDto.getValue().equals("off")) {
				
			}else if(paramDataDto.getValue().equals("on")) {
				if(paramDataDto.getChilds()!=null&&!paramDataDto.getChilds().isEmpty()) {//子比对解析
					paramDataDto.setCompare(1);
					parseListParam(paramDataDto.getChilds(),resultMap,paramDataDto.getName(),disSeq);
				}else {
					getRealDataByCode(paramDataDto,resultMap,parentName,disSeq);
					
				}
			}else {
				   getRealDataByCode(paramDataDto,resultMap,parentName,disSeq);
			}
		}	
	}
	
	
	/**
	 * 根据编码项抓取网页数据
	 * @param paramDataDto
	 */
	public static void getRealDataByCode(ParamDataDto paramDataDto,Map<String,Object> resultMap,String parentName,Map<String,Object> disSeq) {
		if(StringUtils.isNotBlank(parentName)) {
			parentName = parentName.split(":")[0];
		}
		System.out.println("开始抓取【"+paramDataDto.getCode()+"】-【"+parentName+"-"+paramDataDto.getName()+"】的网页参数数据");
		String code= paramDataDto.getCode();
		String[] arr = code.split("-");//code   -----portalarm-6-3
		String seq = arr[1];
		paramDataDto.setResult(false);
		
		if(arr[0].equals("port")) {
			List<Integer> seqNg = (List<Integer>) disSeq.get("port");
			if(seqNg!=null&&!seqNg.isEmpty()) {
				seq = getRealSeq(seqNg,Integer.parseInt(seq)) ;
				code = arr[0]+"-"+seq+"-"+arr[2];
			}
		}
		
		if("error".equals(resultMap.get(arr[0]))) {
			paramDataDto.setCompare(2);//获取失败
			paramDataDto.setRealvalue("参数解析异常");
		}else if("off".equals(resultMap.get(arr[0]+"-"+seq))) {
			paramDataDto.setCompare(3);//未开启
			paramDataDto.setRealvalue("未开启");
		}else {
			paramDataDto.setCompare(1);//获取成功
			String realValue = String.valueOf(resultMap.get(code));
			if(realValue==null)realValue="";
			if(realValue.equals(paramDataDto.getValue())) {
				paramDataDto.setResult(true);
			}
			paramDataDto.setRealvalue(realValue);
		}
	}
	
	public static void main(String[] args) {
		System.out.println(getRealSeq(null,6));
	}
	
	/**
	 * 	获取集合中比当前序号小的个数后重排序
	 *  disSeq ：1,5
	 *  1,2,3,4,5,6
   	  *	seq 2,3,4,6 
	 */
	public static String  getRealSeq(List<Integer> disSeq,Integer seq) {
		disSeq  = new ArrayList<Integer>();
		disSeq.add(1);
		disSeq.add(5);
		Integer newSeq = seq;
		List<Integer> minSeq = disSeq.stream().filter(e->{
			return e<seq;
		}).collect(Collectors.toList());
		if(!minSeq.isEmpty()&&minSeq.size()>0) {
			newSeq  = newSeq - minSeq.size();
		}
		return String.valueOf(newSeq);
	}
	
	public static void parseDevDataToLog(ParamDataDto paramDataDto,Map<String,Object> extend,String parentName) {
		if(paramDataDto.getCompare()==0)return;
		StringBuilder sb = (StringBuilder) extend.get("data");
		String[] arr = parseDescByType( paramDataDto, extend, parentName);
		//先追加菜单
		String menu = (String) extend.get("menu");
		Map<String,String> result = getMenuByCode(paramDataDto.getCode());
		if(!result.get("menu").equals(menu)) {
			sb.append(result.get("name"));
			extend.put("menu", result.get("menu"));
		}
		//追加描述信息
		sb.append(MessageFormat.format(templateData, arr));
		extend.put("code", "1");
	}
	
	/**
	 * 根据类型解析不同结果数据
	 */
	public static String[] parseDescByType(ParamDataDto paramDataDto,Map<String,Object> extend,String parentName) {
		Integer compareType = paramDataDto.getCompare();
		if(StringUtils.isNotBlank(parentName)) {
			parentName = parentName.split(":")[0];
		} 
		String[] array = new String[6];
		array[0] = parentName;
		array[1] = paramDataDto.getName();
		if(compareType==1) {//获取成功
			array[2] = "<span class='acok'>成功</span>";
		}else {
			array[2] = "<span class='acng'>成功</span>";
		}
		array[3] = paramDataDto.getValue();
		array[4] = paramDataDto.getRealvalue();
		if(compareType==1) {
			if(paramDataDto.getResult()) {
				array[5] = "<span class='finalok'>一致</span>";
			}else {
				array[5] = "<span class='finalng'>不一致</span>";
				extend.put("type", "0");// 最终结果失败
			}
		}else {//获取成功
			array[5] = "<span>未知</span>";
			extend.put("type", "0");//最终结果失败
		}
		return array;
	}
	
	/**
	 * 获取标题
	 * @param code
	 * @return
	 */
	public static Map<String,String> getMenuByCode(String code) {
		Map<String,String> map = new HashMap<String,String>();
		String name = "";
		String menu = "";
		
		
		
		System.out.println("==============>"+code);
		
		if(code.contains("server-")) {
			name = "服务器配置";
			menu = "server";
		}else if(code.contains("port-")) {
			name = "串口配置";
			menu = "port";
		}else if(code.contains("portalarm-")) {
			name = "串口报警";
			menu = "portalarm";
		}else if(code.contains("alarm-")) {
			name = "AI报警配置";
			menu = "alarm";
		}else if(code.contains("audio-")) {
			name = "音频配置";
			menu = "audio";
		}else {
			
		}
		map.put("name", MessageFormat.format(splitData, name));
		map.put("menu", menu);
		return map;
	}
	
}